/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package lu.med.praxiswahl.wahlumed.service.beans;

import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import lu.med.praxiswahl.wahlumed.dao.beans.daointerface.DoctorsDAOInt;
import lu.med.praxiswahl.wahlumed.model.beans.DoctorsTBL;
import lu.med.praxiswahl.wahlumed.service.beans.serviceinterface.DoctorServiceInt;

/**
 * 
 * @author pkendzo
 */
@Stateless
@Remote({ DoctorServiceInt.class })
public class DoctorService implements DoctorServiceInt {

	@EJB
	private DoctorsDAOInt docDao;

	private DoctorsTBL tmpDoc;

	public DoctorService() {

		tmpDoc = new DoctorsTBL();

	}

	public int createAddress(String nachname, String vorname, String addresse,
			String email, String telefon, String fax, String fachgebiet) {
		tmpDoc.setNachname(nachname);
		tmpDoc.setVorname(vorname);
		tmpDoc.setAdresse(addresse);
		tmpDoc.setEmail(email);
		tmpDoc.setTelefon(telefon);
		tmpDoc.setFax(fax);
		tmpDoc.setFachgebiet(fachgebiet);

		docDao.persist(tmpDoc);
		return RET_OPERATION_DONE_PROPERLY;
	}

	public int saveHausArtz(Long id) {
		tmpDoc.setId(id);
		docDao.update(tmpDoc);
		return 0;
	}

	/* ############ getter Methoden ############ */

	public Long getID() {
		return tmpDoc.getId();
	}

	public void getDoctor(Long id) {
		tmpDoc = docDao.retrieveAddressByID(id);
	}

	public String getNachname(Long id) {
		// return docDao.retrieveDoctorByID(id).getNachname();
		return tmpDoc.getNachname();
	}

	public String getVorname(Long id) {
		// return docDao.retrieveDoctorByID(id).getVorname();
		return tmpDoc.getVorname();
	}

	public String getAddresse(Long id) {
		// return docDao.retrieveDoctorByID(id).getAdresse();
		return tmpDoc.getAdresse();
	}

	public String getEmail(Long id) {
		// return docDao.retrieveDoctorByID(id).getEmail();
		return tmpDoc.getEmail();
	}

	public String getTelefon(Long id) {
		// return docDao.retrieveDoctorByID(id).getTelefon();
		return tmpDoc.getTelefon();
	}

	public String getFax(Long id) {
		// return docDao.retrieveDoctorByID(id).getFax();
		return tmpDoc.getFax();
	}

	public DoctorsTBL retrieveAddressById(Long id) {
		return docDao.retrieveAddressByID(id);
	}

	public String getFachgebiet(Long id) {
		// return docDao.retrieveDoctorByID(id).getFachgebiet();
		return tmpDoc.getFachgebiet();
	}

	/* ############ Setter Methoden ############ */

	public void setNachname(String nachname) {
		tmpDoc.setNachname(nachname);
	}

	public void setVorname(String vorname) {
		tmpDoc.setVorname(vorname);
	}

	public void setAddresse(String addresse) {
		tmpDoc.setAdresse(addresse);
	}

	public void setEmail(String email) {
		tmpDoc.setEmail(email);
	}

	public void setTelefon(String telefon) {
		tmpDoc.setTelefon(telefon);
	}

	public void setFax(String fax) {
		tmpDoc.setFax(fax);
	}

	public void setFachgebiet(String fachgebiet) {
		tmpDoc.setFachgebiet(fachgebiet);
	}

	/* #################################################### */

	public Long[] hausAerzteAnzeigen() {
		List<DoctorsTBL> doctors = docDao.listAvailableDoctors();
		Long[] docID = new Long[doctors.size()];

		for (int i = 0; i < doctors.size(); i++) {
			docID[i] = doctors.get(i).getId();
		}
		return docID;
	}

	public int deleteHausArtz(Long id) {
		docDao.delete(id);
		return RET_OPERATION_DONE_PROPERLY;
	}

	@Override
	public DoctorsTBL[] retrieveAllAddress() {
		Object[] doctors = docDao.listAvailableDoctors().toArray();
		DoctorsTBL[] eintraege = new DoctorsTBL[doctors.length];

		for (int i = 0; i < doctors.length; i++) {
			eintraege[i] = (DoctorsTBL) doctors[i];
		}
		return eintraege;
	}

	@Override
	public List<DoctorsTBL> listDoctorsItems() {
		List<DoctorsTBL> tmpList = docDao.listAvailableDoctors();

		return tmpList;
	}

	@Override
	public DoctorsTBL[] retrieveAddressEntry(String searchStr) {
		Object[] doctors = docDao.retrieveAddressEntry(searchStr).toArray();
		DoctorsTBL[] eintraege = new DoctorsTBL[doctors.length];

		for (int i = 0; i < doctors.length; i++) {
			eintraege[i] = (DoctorsTBL) doctors[i];
		}
		return eintraege;
	}

	@Override
	public void creatOrSaveAddressEntry(DoctorsTBL entry) {
		
		if(entry.getId() != null){
			// update
			DoctorsTBL docTmp = this.retrieveAddressById(entry.getId());
			docTmp.setNachname(entry.getNachname());
			docTmp.setVorname(entry.getVorname());
			docTmp.setFachgebiet(entry.getFachgebiet());
			docTmp.setUcmCode(entry.getUcmCode());
			docTmp.setTelefon(entry.getTelefon());
			docTmp.setFax(entry.getFax());
			docTmp.setEmail(entry.getEmail());
			docTmp.setWebsite(entry.getWebsite());
			docTmp.setAdresse(entry.getAdresse());
			docTmp.setLastEditor(entry.getLastEditor());
			docTmp.setLastEditTime(entry.getLastEditTime());
			
			docDao.update(docTmp);
		}else{
			// Create
			docDao.persist(entry);
		}
		
		
	}

}
